package com.wego.common.tk.plus.provider;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Set;

/**
 * @className: BatchUpdateByIdProvider
 * @description: TODO
 * @author: Li Chuanwei
 * @date: 2023/07/10 12:20
 * @Company: Copyright [日期] by [作者或个人]
 **/
public class BatchUpdateByIdProvider extends MapperTemplate {
    public BatchUpdateByIdProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }


    public String batchUpdate(MappedStatement statement) {
        //1.获取实体类对应的Class对象
        Class<?> entityClass = super.getEntityClass(statement);

        //2.获取实体类在数据库中对应的表名
        String tableName = super.tableName(entityClass);

        //3.生成update子句
        String update = SqlHelper.updateTable(entityClass, tableName);

        //4.创建StringBuilder用于拼接SQL语句的各个组成部分
        StringBuilder sb = new StringBuilder(update);

        sb.append("<trim prefix=\"set\" suffixOverrides=\",\">");

        //5.获取所有字段信息
        Set<EntityColumn> columns = EntityHelper.getColumns(entityClass);

        //获取主键
        EntityColumn pkEntityColumn = null;
        for (EntityColumn entityColumn : columns) {
            boolean isPrimaryKey = entityColumn.isId();
            if (isPrimaryKey) {
                pkEntityColumn = entityColumn;
                break;
            }
        }

        for (EntityColumn entityColumn : columns) {
            boolean isPrimaryKey = entityColumn.isId();
            //6.判断当前字段是否为主键
            if (!isPrimaryKey) {
                //7.使用非主键字段拼接SET子句
                String columnHolder = entityColumn.getColumnHolder("item");

                sb.append("<trim prefix=\"").append(entityColumn.getColumn()).append("= case\" suffix=\"end, \">");

                sb.append("<foreach collection=\"list\" index=\"index\" item=\"item\">");

                sb.append(" when ")
                        .append(pkEntityColumn.getColumn()).append(" = ").append(pkEntityColumn.getColumnHolder("item"))
                        .append(" then ").append(columnHolder);

                sb.append("</foreach>");

                sb.append("</trim>");
            }
        }
        sb.append("</trim>");

        //10.使用前面缓存的主键名、主键值拼接where子句
        sb.append("where ").append(pkEntityColumn.getColumn()).append(" in ");
        sb.append("<foreach close=\")\" collection=\"list\" item=\"item\" open=\"(\" separator=\", \">");
        sb.append(" #{item.").append(pkEntityColumn.getProperty()).append("}");
        sb.append("</foreach>");

        //11.将拼接好的字符串返回
        return sb.toString();
    }

}
